package com.qdc.datacenters.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qdc.datacenters.domain.entity.BaseEntity;
import com.qdc.datacenters.domain.entity.SysDept;
import com.qdc.datacenters.domain.entity.SysMenu;
import com.qdc.datacenters.domain.entity.SysRole;
import com.qdc.datacenters.domain.query.dept.SysDeptAddQuery;
import com.qdc.datacenters.domain.query.dept.SysDeptQuery;
import com.qdc.datacenters.domain.vo.dept.SysDeptVo;
import com.qdc.datacenters.enums.DeleteStatusEnum;
import com.qdc.datacenters.mapper.SysDeptMapper;
import com.qdc.datacenters.service.ISysDeptService;
import com.qdc.datacenters.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 部门信息表(SysDept)表服务实现类
 *
 * @author danmo
 * @since 2024-02-26 17:31:45
 */
@Service
public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {

    @Override
    public void add(SysDeptAddQuery query) {
        SysDept sysDept = new SysDept();
        BeanUtils.copyProperties(query,sysDept);
        save(sysDept);
    }

    @Override
    public void edit(SysDeptAddQuery query) {
        SysDept sysDept = new SysDept();
        BeanUtils.copyProperties(query,sysDept);
        updateById(sysDept);
    }

    @Override
    public SysDept get(SysDeptQuery query) {
        return getById(query.getDeptId());
    }

    @Override
    public List<SysDept> list(SysDeptQuery query) {
        return this.baseMapper.getList(query);
    }

    @Override
    public List<Tree<Long>> buildDeptTree(List<SysDept> list) {
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        //自定义属性名 都要默认值的
        treeNodeConfig.setWeightKey("order");
        treeNodeConfig.setIdKey("deptId");
        treeNodeConfig.setParentIdKey("parentId");
        return TreeUtil.build(list, 0L, treeNodeConfig, ((sysDept, treeNode) -> {
            treeNode.setId(sysDept.getDeptId());//id
            treeNode.setParentId(sysDept.getParentId());//父id
            treeNode.putExtra("deptName", sysDept.getDeptName());
            treeNode.putExtra("order", sysDept.getOrder());
            treeNode.putExtra("status", sysDept.getStatus());
            treeNode.putExtra("remark", sysDept.getRemark());
        }));
    }

    @Override
    public void delete(SysDeptQuery query) {

        Optional.ofNullable(query.getDeptIds()).orElseGet(ArrayList::new).add(query.getDeptId());
        if (!CollectionUtils.isEmpty(query.getDeptIds())) {
            update(new LambdaUpdateWrapper<SysDept>().set(BaseEntity::getDelFlag, 1)
                    .set(BaseEntity::getUpdateTime,new Date())
                    .set(BaseEntity::getUpdateBy, SecurityUtils.getUserId())
                    .and(item -> item.in(SysDept::getDeptId, query.getDeptIds()))
                    .or().in(SysDept::getParentId,query.getDeptIds()));
        }
    }

    @Override
    public List<SysDeptVo> getDeptByUserId(Long userId) {
        return this.baseMapper.getDeptByUserId(userId);
    }
}

